/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheart.server;

import gameheart.transport.Command;
import gameheart.transport.CommandIds;
import gameheart.transport.CommandMessageTag;
import gameheart.transport.MyException;
import gameheart.transport.Transporter;



/**
 *
 * @author win7
 */
public class ServerAdapter implements Runnable {

    /**
     * ten cua nguoi choi hien tai
     */
    private String _userName = "";
    private ServerController _controller = null;
    private Transporter _transporter;

    public ServerAdapter(Transporter transporter, ServerController controller) {
        _controller = controller;
        _transporter = transporter;
    }

    /**
     * nhan va phan tich lenh
     * @throws MyException
     */
    public void processMessage() throws MyException {
        while (true) {
            Command command = _transporter.GetCommand(true);
            dispathCommand(command);
        }
    }

    /**
     * phan phoi lenh den cac ham xu ly
     * @param command
     * @throws MyException
     */
    private void dispathCommand(Command command) throws MyException {
        getController().receiveMessage(this, command);
    }

    /**
     * goi yeu cau client thuc hien nuoc di
     * @throws MyException
     */
    public void SendPlayRequest() throws MyException {
        SendCommand(CommandIds.requestPlayId, null, null, 0);
        System.out.println("Server : yeu cau nguoi choi thuc hien nuoc di.");
    }

    /**
     * thong bao ket qua nuoc di cua client
     * @param valid tree : nuoc di hop le. false : khong hop le.
     * @throws MyException
     */
    public void SendResultPlay(boolean valid) throws MyException {
        String[] argNames = new String[1];
        String[] argValues = new String[1];
        argNames[0] = CommandMessageTag.ResultTag;
        if (valid) {
            argValues[0] = "1";
        } else {
            argValues[0] = "0";
        }
        SendCommand(CommandIds.PlayResult, argNames, argValues, 1);
    }

    /**
     * goi thong bao co nguoi choi vua thuc hien nuoc di den client
     * @param card quan bai nguoi choi danh
     * @param userID nguoi choi vua thuc hien nuoc di
     * @throws MyException
     */
    public void SendDrawMovingCard(ServerCard card, int userID) throws MyException {
        int argNumber = 3;
        String[] argNames = new String[argNumber];
        String[] argValues = new String[argNumber];
        argNames[0] = CommandMessageTag.UserIdTag;
        argValues[0] = String.valueOf(userID);
        argNames[1] = CommandMessageTag.CardNumberTag;
        argValues[1] = String.valueOf(card.getDiem());
        argNames[2] = CommandMessageTag.CardTypeTag;
        argValues[2] = String.valueOf(card.getType().ordinal());
        SendCommand(CommandIds.requestDrawPlayCardId, argNames, argValues, argNumber);
    }


    public void run() {
        try {
            processMessage();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    private int userID;

    /**
     * gan user id cho adapter. user id tuong ung voi adapter hien tai.
     * @param id user id
     */
    public void setUserID(int id) {
        userID = id;
    }

    /**
     * lay user id tuong ung voi adapter hien tai
     * @return user id
     */
    public int getUserID() {
        return userID;
    }

    /**
     *goi den client user id cua nguoi choi tuong ung voi adapter hien tai.
     * @param userId
     * @throws MyException
     */
    public void SendUserIdOfPlayer(int userId) throws MyException {
        int argNumber = 1;
        String[] argNames = new String[argNumber];
        String[] argValues = new String[argNumber];
        argNames[0] = CommandMessageTag.UserIdTag;
        argValues[0] = String.valueOf(userId);
        SendCommand(CommandIds.CurrentUserId, argNames, argValues, argNumber);
    }

    /**
     * goi thong bao da het mot vong choi
     * @param getCardUserId nguoi choi nhan bai cua vong choi.
     * @throws MyException
     */
    public void SendRoundTripEnd(int getCardUserId) throws MyException {
        int argNumber = 1;
        String[] argNames = new String[argNumber];
        String[] argValues = new String[argNumber];
        argNames[0] = CommandMessageTag.UserIdTag;
        argValues[0] = String.valueOf(getCardUserId);
        SendCommand(CommandIds.RoundTripEndId, argNames, argValues, argNumber);
    }

    /**
     * van bai ket thuc
     * @throws MyException
     */
    public void SendGameEnd() throws MyException {
        SendCommand(CommandIds.GameEndId, null, null, 0);
    }
    public void SendPointInHand(int point) throws MyException
    {
        String[] argName ={CommandMessageTag.MarkTag};
        String[] argValue = {String.valueOf(point)};
        SendCommand(CommandIds.PointInHand, argName, argValue, 1);
    }
    /**
     * goi so diem trong tro choi  cua nguoi choi
     * @param currentMarks
     * @throws MyException
     */
    public void SendMarkOfPlayers(int[] marks) throws MyException {
        int argNumber = 4;
        String[] argNames = new String[argNumber];
        String[] argValues = new String[argNumber];
        for(int index = 0; index < 4 ; ++ index)
        {
            argNames[index] = CommandMessageTag.MarkTag;
            argValues[index] = String.valueOf(marks[index]);
        }
        SendCommand(CommandIds.MarkInGame, argNames, argValues, argNumber);
    }

    public void SendTotalMarkOfPlayer(int[] totalMarks) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * goi user name cua cac nguoi choi den client.
     * @param userNames user name cua cac nguoi choi.
     * @throws MyException
     */
    public void SendUserNames(String[] userNames) throws MyException {
        int argNumber = userNames.length;
        String[] argName = new String[argNumber];
        for (int index = 0; index < argNumber; ++index) {
            argName[index] = CommandMessageTag.UserNameTag;
        }
        SendCommand(CommandIds.UseNameOfPlayers, argName, userNames, argNumber);
    }

    /**
     * goi lenh den client
     * @param commandId ma lenh
     * @param argName  cac ten cua tham so
     * @param argValue cac gia tri cua tham so
     * @param argNumber so tham so
     * @throws MyException
     */
    public synchronized  void SendCommand(int commandId, String[] argName, String[] argValue, int argNumber) throws MyException {
        Command command = new Command();
        command.setCommandId(commandId);
        command.setArgNumber(argNumber);
        command.setArgNames(argName);
        command.setArgdValues(argValue);
        _transporter.SendCommand(command);
    }

    
    /**
     * chia bai cho nguoi choi
     * @param cards quan bai duoc chia cho nguoi choi.
     * @throws MyException
     */
    public void sendDealRequest(ServerCard[] cards) throws MyException {
        String[] argName = new String[cards.length * 2];
        String[] argValue = new String[cards.length * 2];
        for (int index = 0; index < cards.length; ++index) {
            argName[index * 2] = CommandMessageTag.CardNumberTag;
            argValue[index * 2] = String.valueOf(cards[index].getDiem());
            argName[index * 2 + 1] = CommandMessageTag.CardTypeTag;
            argValue[index * 2 + 1] = String.valueOf(cards[index].getType().ordinal());
        }
        SendCommand(CommandIds.DealRequestId, argName, argValue, cards.length * 2);
    }

    /**
     * lay doi tuong quan ly server
     * @return the _controller
     */
    public ServerController getController() {
        return _controller;
    }

    /**
     * gan doi tuong quanl y server
     * @param controller the _controller to set
     */
    public void setController(ServerController controller) {
        this._controller = controller;
    }

    /**
     * ket thong bao den client ket qua join game
     * @param accept
     * @throws MyException
     */
    public void SendJoinResult(boolean accept) throws MyException {
        String[] argNames = new String[1];
        String[] argValues = new String[1];
        argNames[0] = CommandMessageTag.JoinGameResultTag;
        argValues[0] = String.valueOf(accept);
        SendCommand(CommandIds.JoinGameResult, argNames, argValues, 1);
    }
    public void SendPassCardRequest(Direction direction) throws MyException
    {
        String[] argNames = new String[1];
        String[] argValues = new String[1];
        argNames[0] = CommandMessageTag.DirectionTag;
        argValues[0] = String.valueOf(direction.ordinal());
        SendCommand(CommandIds.PassCardRequest, argNames, argValues, 1);
    }

    /**
     * lay user name nguoi choi tuong ung voi adapter hien tai.
     * @return the userName
     */
    public String getUserName() {
        return _userName;
    }

    /**
     * gan user name cua nguoi choi tuong ung voi adapter hien tai.
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this._userName = userName;
    }

    /**
     * goi ket qua sau khi da tat ca client da pass card.
     * @param cards quan bai cua nguoi choi sua khi pass card
     * @throws MyException
     */
    public void SendPassCardDone(ServerCard[] cards) throws MyException {
        String[] argName = new String[cards.length * 2];
        String[] argValue = new String[cards.length * 2];
        for (int index = 0; index < cards.length; ++index) {
            argName[index * 2] = CommandMessageTag.CardNumberTag;
            argValue[index * 2] = String.valueOf(cards[index].getDiem());
            argName[index * 2 + 1] = CommandMessageTag.CardTypeTag;
            argValue[index * 2 + 1] = String.valueOf(cards[index].getType().ordinal());
        }
        SendCommand(CommandIds.passCardDone, argName, argValue, cards.length * 2);
    }
    public void SendUserJoinedGameNotify(int userId, String userName) throws MyException
    {
        String[] argNames = new String[2];
        String[] argValues = new String[2];
        argNames[0] = CommandMessageTag.UserIdTag;
        argValues[0] = String.valueOf(userId);
        argNames[1] = CommandMessageTag.UserNameTag;
        argValues[1] = userName;
        SendCommand(CommandIds.userJoinedGame, argNames, argValues, 2);
    }
    public void SendChatMessage(int userId, String message) throws MyException
    {
       int argNumber = 2;
       String[] argNames = {CommandMessageTag.UserIdTag, CommandMessageTag.MessageTag};
       String[] argValues ={String.valueOf(userId), message};
       SendCommand(CommandIds.Chat, argNames, argValues, argNumber);
    }
}
